package offer;

import java.util.Iterator;

import com.netscenery.yang.model.*;
/*
 * lervel ����ṹΪ��lervel[0] = Module.name,��һ�������ҽ������ͺ͵�һ�������
 * lervel[1] Ϊ��һ������ ��
 * ����
 * lervel[n] Ϊ��n-1����ӽڵ���
 *              0       1            2       3      4      5
 *  �磺Module.name_configuration_netconf_server_status_disable
 */
public class GetNodeFromModule {
	
	public static ModelElement getItemNode(Module module , String[] lervel ){
		
		String[] names = lervel[0].split("@");
		if("Module".equalsIgnoreCase(names[0])){
			return module;
		}else if("ModelElement".equalsIgnoreCase(names[0])){
			return GetNodeFromModule.getModuleElement(module, lervel);
		}else if("Note".equalsIgnoreCase(names[0])){
			return GetNodeFromModule.getDataDefByLervels(module, lervel);
		}
		return null;
	}
/******************************************************************************************
*                                Note ������       Begin                                *
* *****************************************************************************************/
	public static DataDefElement getDataDefByLervels(Module module, String[] lervel){
		DataDefElement itemNode = null;
		if(0 < module.getDataDefsNumber()){
//			Iterator<Node> item = module.getNodes().iterator();
//			while(item.hasNext()){
//				itemNode = item.next();
//				if(itemNode.getName().equalsIgnoreCase(lervel[1])){
//					break;
//				}
//			}
			for(int i = 0 ; i < module.getDataDefsNumber(); i++){
				if(module.getDataDef(i).getName().equalsIgnoreCase(lervel[1])){
					itemNode = module.getDataDef(i);
					break;
				}
			}
		}
		//���ǰ�����Module�µĵ�һ�㣬������ҪѰ�����ӽڵ㡣
		if(lervel.length == 2){
			return itemNode;
		}
		
		return searchDataDefByLervels((Node) itemNode,lervel,2);
		
	}
	
	private static DataDefElement  searchDataDefByLervels(Node itemNode, String[] lervel , int currentLever){
		
		DataDefElement tempNode = null;
		if((0 != (itemNode).getSonsNumber())){
//			Iterator<Node> item = itemNode.getSons().iterator();
//			while(item.hasNext()){
//				tempNode = item.next();
//				if(tempNode.getName().equalsIgnoreCase(lervel[currentLever])){
//					break;
//				}
//			}
			for(int i = 0; i < itemNode.getSonsNumber(); i++){
				tempNode = itemNode.getSon(i);
				if(tempNode.getName().equalsIgnoreCase(lervel[currentLever])){
					break;
				}
			}
		}
		
		if((null == tempNode)||(currentLever == lervel.length -1)){
			return tempNode;
		}
		else{
			return searchDataDefByLervels((Node) tempNode,lervel,++currentLever);
		}
	}
 /******************************************************************************************
 *                                Note ������       End                                   *
 * *****************************************************************************************/

	
/* ******************************************************************************************
 *                                Element ������  Begin                                     *
 * ******************************************************************************************/
	/*
	 * lervel ����ṹΪ��lervel[0] = ModelElement.����.name,��һ�������ҽ������ͺ͵�һ�������
	 * lervel[1] Ϊ��һ������ ��
	 * ����
	 * lervel[n] Ϊ��n-1����ӽڵ���
	 *     0       1            2       3      4      5
	 *  �磺ModelElement@name\configuration\netconf\server\status\disable
	 */
	public static ModelElement getModuleElement(Module module, String[] lervel){
		String[] names = lervel[0].split("@");
		if(names.length < 2){
			return null;
		}
		ModelElement  element = null; 
		if(names[1].equalsIgnoreCase("Include")){
			for(int i = 0 ; i < module.getIncludesNumber() ; i++ ){
				element = module.getInclude(i);
				if(names[2].equalsIgnoreCase(element.getName())){
					break;
				}
			}
		}else if(names[1].equalsIgnoreCase("Import")){
			for(int i = 0 ; i < module.getImportsNumber() ; i++ ){
				element = module.getImport(i);
				if(names[2].equalsIgnoreCase(element.getName())){
					break;
				}
			}
		}else if(names[1].equalsIgnoreCase("Revision")){
			for(int i = 0 ; i < module.getRevisionsNumber() ; i++ ){
				element = module.getRevision(i);
				if(names[2].equalsIgnoreCase(element.getName())){
					break;
				}
			}
		}else if(names[1].equalsIgnoreCase("Extension")){
			for(int i = 0 ; i < module.getExtensionsNumber() ; i++ ){
				element = module.getExtension(i);
				if(names[2].equalsIgnoreCase(element.getName())){
					break;
				}
			}
		}else if(names[1].equalsIgnoreCase("Typedef")){
			for(int i = 0 ; i < module.getTypedefsNumber() ; i++ ){
				element = module.getTypedef(i);
				if(names[2].equalsIgnoreCase(element.getName())){
					break;
				}
			}
			
			//˵����ʱ���ҵ��� Typedef ���ӽڵ�
			//Ŀǰtypedefֻ��������
			if(lervel.length > 1){
				//���ǰTypedef��û��Type�����򷵻ؿ�
				if(null == ((Typedef)element).getType()){
					return null;
				}
				if(lervel[1].equalsIgnoreCase((((Typedef)element).getType()).getName())){
					element = ((Typedef)element).getType();
				}else{
				//�û��ı�Typedef�е�Type����
					return null;
				}
			}
		}else if(names[1].equalsIgnoreCase("Feature")){
			// deal with ModelElement.Feature.MyFeature
			for(int i = 0; i < module.getFeaturesNumber() ; i++){
				element = module.getFeature(i);
				if(names[2].equalsIgnoreCase(element.getName())){
					break;
				}
			}
		}else if(names[1].equalsIgnoreCase("Deviation")){
			// deal with ModelElement.Deviation.MyDeviation
			for(int i = 0; i < module.getDeviationsNumber() ; i++){
				element = module.getDeviation(i);
				if(names[2].equalsIgnoreCase(element.getName())){
					break;
				}
			}
			//��һ������Deviation������ڵ�
			if(lervel.length > 1){
				Deviation deviation = (Deviation)element;
				if(0 == deviation.getDeviatesNumber()){
					return null;
				}
				//����Deviate
				for(int i = 0 ; i < deviation.getDeviatesNumber(); i++){
					if(lervel[1].equalsIgnoreCase(deviation.getDeviate(i).getName())){
						element = deviation.getDeviate(i);
						break;
					}
				}
				if(!(element instanceof Deviate)){
					return null;
				}
				//����Type�ڵ�
				if((element instanceof Deviate)&& (lervel.length > 2)){
					if(lervel[2].equalsIgnoreCase((((Typedef)element).getType()).getName())){
						element = ((Deviate)element).getType();
					}else{
					//�û��ı�Deviate�е�Type����
						return null;
					}
				}
			}
		}
		return element;
	}
/* ******************************************************************************************
 *                                Element ������  End                                       *
 * ******************************************************************************************/		
}
